package top.fullj.win32;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;

/**
 * @author bruce.wu
 * @since 2019/11/14 9:22
 */
public interface Kernel32 extends StdCallLibrary {

    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);

    /** https://docs.microsoft.com/zh-cn/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror */
    int GetLastError();

    /** https://docs.microsoft.com/zh-cn/windows/win32/api/fileapi/nf-fileapi-createfilea */
    Pointer CreateFileA(String lpFileName,
                        int dwDesiredAccess,
                        int dwShareMode,
                        Pointer lpSecurityAttributes,
                        int dwCreationDisposition,
                        int dwFlagsAndAttributes,
                        Pointer hTemplateFile);
    /** https://docs.microsoft.com/zh-cn/windows/win32/api/handleapi/nf-handleapi-closehandle */
    boolean CloseHandle(Pointer hObject);

    /** https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setupcomm */
    boolean SetupComm(Pointer hFile, int dwInQueue, int dwOutQueue);
    /** https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommtimeouts */
    boolean GetCommTimeouts(Pointer hFile, COMMTIMEOUTS lpCommTimeouts);
    /** https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setcommtimeouts */
    boolean SetCommTimeouts(Pointer hFile, COMMTIMEOUTS lpCommTimeouts);
    /** https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommstate */
    boolean GetCommState(Pointer hFile, DCB lpDCB);
    /** https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setcommstate */
    boolean SetCommState(Pointer hFile, DCB lpDCB);

    /** https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-purgecomm */
    boolean PurgeComm(Pointer hFile, int dwFlags);
    /** https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-clearcommerror */
    boolean ClearCommError(Pointer hFile, IntByReference lpErrors, COMSTAT lpStat);

    /** https://docs.microsoft.com/zh-cn/windows/win32/api/fileapi/nf-fileapi-writefile */
    boolean WriteFile(Pointer hFile,
                      byte[] lpBuffer,
                      int nNumberOfBytesToWrite,
                      IntByReference lpNumberOfBytesWritten,
                      Pointer lpOverlapped);
    /** https://docs.microsoft.com/zh-cn/windows/win32/api/fileapi/nf-fileapi-readfile */
    boolean ReadFile(Pointer hFile,
                     byte[] lpBuffer,
                     int nNumberOfBytesToRead,
                     IntByReference lpNumberOfBytesRead,
                     Pointer lpOverlapped);
}
